home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gas / src138.zoo / atof-vax.c < prev    next >
C/C++ Source or Header  |  1991-01-21  |  14KB  |  514 lines

  1. /* atof_vax.c - turn a Flonum into a VAX floating point number
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.     /* JF added these two for md_atof() */
  21. #include "as.h"
  22. #include "read.h"
  23.  
  24. #include "flonum.h"
  25.  
  26.  
  27.                 /* Precision in LittleNums. */
  28. #define MAX_PRECISION (8)
  29. #define H_PRECISION (8)
  30. #define G_PRECISION (4)
  31. #define D_PRECISION (4)
  32. #define F_PRECISION (2)
  33.  
  34.                 /* Length in LittleNums of guard bits. */
  35. #define GUARD (2)
  36.  
  37. int                /* Number of chars in flonum type 'letter'. */
  38. atof_vax_sizeof (letter)
  39.      char letter;
  40. {
  41.   int    return_value;
  42.  
  43.   /*
  44.    * Permitting uppercase letters is probably a bad idea.
  45.    * Please use only lower-cased letters in case the upper-cased
  46.    * ones become unsupported!
  47.    */
  48.   switch (letter)
  49.     {
  50.     case 'f':
  51.     case 'F':
  52.       return_value = 4;
  53.       break;
  54.  
  55.     case 'd':
  56.     case 'D':
  57.     case 'g':
  58.     case 'G':
  59.       return_value = 8;
  60.       break;
  61.  
  62.     case 'h':
  63.     case 'H':
  64.       return_value = 16;
  65.       break;
  66.  
  67.     default:
  68.       return_value = 0;
  69.       break;
  70.     }
  71.   return (return_value);
  72. }                /* atof_vax_sizeof */
  73.  
  74. static const long int mask [] = {
  75.   0x00000000,
  76.   0x00000001,
  77.   0x00000003,
  78.   0x00000007,
  79.   0x0000000f,
  80.   0x0000001f,
  81.   0x0000003f,
  82.   0x0000007f,
  83.   0x000000ff,
  84.   0x000001ff,
  85.   0x000003ff,
  86.   0x000007ff,
  87.   0x00000fff,
  88.   0x00001fff,
  89.   0x00003fff,
  90.   0x00007fff,
  91.   0x0000ffff,
  92.   0x0001ffff,
  93.   0x0003ffff,
  94.   0x0007ffff,
  95.   0x000fffff,
  96.   0x001fffff,
  97.   0x003fffff,
  98.   0x007fffff,
  99.   0x00ffffff,
  100.   0x01ffffff,
  101.   0x03ffffff,
  102.   0x07ffffff,
  103.   0x0fffffff,
  104.   0x1fffffff,
  105.   0x3fffffff,
  106.   0x7fffffff,
  107.   0xffffffff
  108.   };
  109.  
  110.  
  111. /* Shared between flonum_gen2vax and next_bits */
  112. static int        bits_left_in_littlenum;
  113. static LITTLENUM_TYPE *    littlenum_pointer;
  114. static LITTLENUM_TYPE * littlenum_end;
  115.  
  116. static int
  117. next_bits (number_of_bits)
  118.      int        number_of_bits;
  119. {
  120.   int            return_value;
  121.  
  122.   if(littlenum_pointer<littlenum_end)
  123.       return 0;
  124.   if (number_of_bits >= bits_left_in_littlenum)
  125.     {
  126.       return_value  = mask [bits_left_in_littlenum] & * littlenum_pointer;
  127.       number_of_bits -= bits_left_in_littlenum;
  128.       return_value <<= number_of_bits;
  129.       bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  130.       littlenum_pointer --;
  131.       if(littlenum_pointer>=littlenum_end)
  132.           return_value |= ( (* littlenum_pointer) >> (bits_left_in_littlenum) ) & mask [number_of_bits];
  133.     }
  134.   else
  135.     {
  136.       bits_left_in_littlenum -= number_of_bits;
  137.       return_value = mask [number_of_bits] & ( (* littlenum_pointer) >> bits_left_in_littlenum);
  138.     }
  139.   return (return_value);
  140. }
  141.  
  142. static void
  143. make_invalid_floating_point_number (words)
  144.      LITTLENUM_TYPE *    words;
  145. {
  146.   * words = 0x8000;        /* Floating Reserved Operand Code */
  147. }
  148.  
  149. static int            /* 0 means letter is OK. */
  150. what_kind_of_float (letter, precisionP, exponent_bitsP)
  151.      char        letter;    /* In: lowercase please. What kind of float? */
  152.      int *        precisionP; /* Number of 16-bit words in the float. */
  153.      long int *        exponent_bitsP;    /* Number of exponent bits. */
  154. {
  155.   int    retval;            /* 0: OK. */
  156.  
  157.   retval = 0;
  158.   switch (letter)
  159.     {
  160.     case 'f':
  161.       * precisionP = F_PRECISION;
  162.       * exponent_bitsP = 8;
  163.       break;
  164.  
  165.     case 'd':
  166.       * precisionP = D_PRECISION;
  167.       * exponent_bitsP = 8;
  168.       break;
  169.  
  170.     case 'g':
  171.       * precisionP = G_PRECISION;
  172.       * exponent_bitsP = 11;
  173.       break;
  174.  
  175.     case 'h':
  176.       * precisionP = H_PRECISION;
  177.       * exponent_bitsP = 15;
  178.       break;
  179.  
  180.     default:
  181.       retval = 69;
  182.       break;
  183.     }
  184.   return (retval);
  185. }
  186.  
  187. /***********************************************************************\
  188. *                                    *
  189. *    Warning: this returns 16-bit LITTLENUMs, because that is    *
  190. *    what the VAX thinks in. It is up to the caller to figure    *
  191. *    out any alignment problems and to conspire for the bytes/word    *
  192. *    to be emitted in the right order. Bigendians beware!        *
  193. *                                    *
  194. \***********************************************************************/
  195.  
  196. char *                /* Return pointer past text consumed. */
  197. atof_vax (str, what_kind, words)
  198.      char *        str;    /* Text to convert to binary. */
  199.      char        what_kind; /* 'd', 'f', 'g', 'h' */
  200.      LITTLENUM_TYPE *    words;    /* Build the binary here. */
  201. {
  202.   FLONUM_TYPE        f;
  203.   LITTLENUM_TYPE    bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  204.                 /* Extra bits for zeroed low-order bits. */
  205.                 /* The 1st MAX_PRECISION are zeroed, */
  206.                 /* the last contain flonum bits. */
  207.   char *        return_value;
  208.   int            precision; /* Number of 16-bit words in the format. */
  209.   long int        exponent_bits;
  210.  
  211.   return_value = str;
  212.   f . low    = bits + MAX_PRECISION;
  213.   f . high    = NULL;
  214.   f . leader    = NULL;
  215.   f . exponent    = NULL;
  216.   f . sign    = '\0';
  217.  
  218.   if (what_kind_of_float (what_kind, & precision, & exponent_bits))
  219.     {
  220.       return_value = NULL;    /* We lost. */
  221.       make_invalid_floating_point_number (words);
  222.     }
  223.   if (return_value)
  224.     {
  225.       bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  226.  
  227.                 /* Use more LittleNums than seems */
  228.                 /* necessary: the highest flonum may have */
  229.                 /* 15 leading 0 bits, so could be useless. */
  230.       f . high = f . low + precision - 1 + GUARD;
  231.  
  232.       if (atof_generic (& return_value, ".", "eE", & f))
  233.     {
  234.       make_invalid_floating_point_number (words);
  235.       return_value = NULL;    /* we lost */
  236.     }
  237.       else
  238.     {
  239.       if (flonum_gen2vax (what_kind, & f, words))
  240.         {
  241.           return_value = NULL;
  242.         }
  243.     }
  244.     }
  245.   return (return_value);
  246. }
  247.  
  248. /*
  249.  * In: a flonum, a vax floating point format.
  250.  * Out: a vax floating-point bit pattern.
  251.  */
  252.  
  253. int                /* 0: OK. */
  254. flonum_gen2vax (format_letter, f, words)
  255.      char        format_letter; /* One of 'd' 'f' 'g' 'h'. */
  256.      FLONUM_TYPE *    f;
  257.      LITTLENUM_TYPE *    words;    /* Deliver answer here. */
  258. {
  259.   LITTLENUM_TYPE *    lp;
  260.   int            precision;
  261.   long int        exponent_bits;
  262.   int            return_value; /* 0 == OK. */
  263.  
  264.   return_value = what_kind_of_float (format_letter, & precision, & exponent_bits);
  265.   if (return_value != 0)
  266.     {
  267.       make_invalid_floating_point_number (words);
  268.     }
  269.   else
  270.     {
  271.       if (f -> low > f -> leader)
  272.     {
  273.       /* 0.0e0 seen. */
  274.       bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  275.     }
  276.       else
  277.     {
  278.       long int        exponent_1;
  279.       long int        exponent_2;
  280.       long int        exponent_3;
  281.       long int        exponent_4;
  282.       int        exponent_skippage;
  283.       LITTLENUM_TYPE    word1;
  284.  
  285.         /* JF: Deal with new Nan, +Inf and -Inf codes */
  286.       if(f->sign!='-' && f->sign!='+') {
  287.         make_invalid_floating_point_number(words);
  288.         return return_value;
  289.       }
  290.       /*
  291.        * All vaxen floating_point formats (so far) have:
  292.        * Bit 15 is sign bit.
  293.        * Bits 14:n are excess-whatever exponent.
  294.        * Bits n-1:0 (if any) are most significant bits of fraction.
  295.        * Bits 15:0 of the next word are the next most significant bits.
  296.        * And so on for each other word.
  297.        *
  298.        * All this to be compatible with a KF11?? (Which is still faster
  299.        * than lots of vaxen I can think of, but it also has higher
  300.        * maintenance costs ... sigh).
  301.        *
  302.        * So we need: number of bits of exponent, number of bits of
  303.        * mantissa.
  304.        */
  305.       
  306. #ifdef NEVER  /******* This zeroing seems redundant - Dean 3may86 **********/
  307.       /*
  308.        * No matter how few bits we got back from the atof()
  309.        * routine, add enough zero littlenums so the rest of the
  310.        * code won't run out of "significant" bits in the mantissa.
  311.        */
  312.       {
  313.         LITTLENUM_TYPE * ltp;
  314.         for (ltp = f -> leader + 1;
  315.          ltp <= f -> low + precision;
  316.          ltp ++)
  317.           {
  318.         * ltp = 0;
  319.           }
  320.       }
  321. #endif
  322.       
  323.       bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  324.       littlenum_pointer = f -> leader;
  325.       littlenu